package com.tiger.cloud.controller.crm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.form.SysDictSmall;
import com.tiger.cloud.model.crm.LoanRecord;
import com.tiger.cloud.service.crm.ILoanRecordService;
import com.tiger.cloud.service.system.CommonDict;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 合作记录/借款记录
 */
@Controller
@RequestMapping(value = "loanRecord")
@Api(value = "记录/借款记录",tags = "记录/借款记录")
public class LoanRecordController extends BaseController {

	@Autowired
	private ILoanRecordService loanRecordService;
	
	/**
	 * 获取列表默认值
	 */
	@RequestMapping(value = "getDefaultFileds", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "获取列表默认值", notes = "获取列表默认值")
	public Response getDefaultFileds() {
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("loan_record");
		return ResponseUtil.ok(values);
	}
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "增量分页查找合作/借款记录",notes = "增量分页查找合作/借款记录")
	public Response find(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		loanRecord.setUserId(userId);
		loanRecord.setIsDelete(DeleteEnum.NOMAIL);
		Page<LoanRecord> datas=loanRecordService.page(loanRecord);
		return ResponseUtil.ok(datas);
	}
	 
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "根据ID获取单条合作/借款记录",notes = "根据ID获取单条合作/借款记录")
	public Response get(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getId()!=null&&loanRecord.getId()<0){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"id必传!");
		}
		LoanRecord findLoanRecord=loanRecordService.get(loanRecord.getId());
		if(findLoanRecord.getUserId()!=userId){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"只能查看自己的数据!");
		}
		Map<String, List<SysDictSmall>> values = CommonDict.getFileds("loan_record");
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("loanRecord", findLoanRecord);
		maps.put("defaults", values);
		return ResponseUtil.ok(maps);
	}
	
	/**
	 * 增量获取合作/借款记录
	 */
	@RequestMapping(value = "incrementRecommands", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "增量获取合作/借款记录",notes = "增量获取合作/借款记录")
	public Response list(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		loanRecord.setIsDelete(DeleteEnum.NOMAIL);
		loanRecord.setUserId(userId);
		return ResponseUtil.ok(loanRecordService.find(loanRecord));
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "根新合作/借款记录",notes = "合作记录/借款记录")
	public Response update(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getId()!=null&&loanRecord.getId()>0){
			LoanRecord updateEntity=loanRecordService.get(loanRecord.getId());
			if(updateEntity==null||updateEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanRecord.setUserId(userId);
			if(loanRecordService.updateLoanRecord(loanRecord)){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "添加合作/借款记录",notes = "合作记录/借款记录")
	public Response add(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getCustomerId()==null){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"customerId必传!");
		}
		loanRecord.setIsDelete(DeleteEnum.NOMAIL);
		loanRecord.setUserId(userId);
		if(loanRecordService.insertLoanRecord(loanRecord)){
			return ResponseUtil.ok(loanRecord);
		}
		return ResponseUtil.error();
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	@ApiOperation(value = "删除合作/借款记录",notes = "合作记录/借款记录")
	public Response delete(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody LoanRecord loanRecord){
		if(loanRecord.getId()!=null&&loanRecord.getId()>0){
			LoanRecord deleteEntity=loanRecordService.get(loanRecord.getId());
			if(deleteEntity==null||deleteEntity.getUserId()!=userId){
				return ResponseUtil.error("参数错误,请重试!");
			}
			loanRecord.setUserId(userId);
			deleteEntity.setIsDelete(DeleteEnum.DELETE);
			if(loanRecordService.deleteLoanRecord(deleteEntity)){
				return ResponseUtil.ok();
			};
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}